package algorithms;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;

import commons.SimilarityItem;
import commons.UserRatingList;
import commons.UserRatingMap;
import config.ConfigManager;
import datamanagers.DataManagerException;



public class PPSimilarityAlgorithm extends SimilarityAlgorithm {

	@Override
	public SimilarityItem eval(int profileId1, int profileId2)
			throws DataManagerException, NumberFormatException, ConfigurationException {
		UserRatingMap scoresp1 = datamanager.getScoresMap(profileId1);
		UserRatingMap scoresp2 = datamanager.getScoresMap(profileId2);
		SimilarityItem si = new SimilarityItem();
		si.id1 = profileId1;
		si.id2 = profileId2;
		
		double mean_ratingu1 = scoresp1.getMean();
		double mean_ratingu2 = scoresp2.getMean();
		double den, num_sum1, num_sum2;
		int i = 0;
		HashMap<Integer,Double[]>  commonratings= scoresp1.intersect(scoresp2.getMappa());
		den = 0;
		num_sum1 = 0;
		num_sum2 = 0;
		if(commonratings == null){
			this.common_ratings_size=0;	
		}else{
		this.common_ratings_size = commonratings.size();
		
		}// if (num_sum1 != 0 && num_sum2 != 2) {
		if (common_ratings_size != 0) {
			for (int valore: commonratings.keySet()){
				Double [] arrayValori= commonratings.get(valore);
					double rating1 =arrayValori[0]  - mean_ratingu1;;
					double rating2 =arrayValori[1] - mean_ratingu2;
					
					den += rating1 * rating2;
					num_sum1 += rating1 * rating1;
					num_sum2 += rating2 * rating2;
					i++;
					
				}
			si.similarity = (float) (den / Math.sqrt(num_sum1 * num_sum2));
			si.commonratings = common_ratings_size;
			
		}
		return si;
	}

}
